//
//  FetchTrain.m
//  dump_12306
//
//  Created by qiaolun on 5/31/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "FetchTrain.h"
#import "QRegExp.h"


@implementation FetchTrain

+ (NSArray *)code:(NSString *)trainCode dateAt:(NSString *)dt;{
	NSMutableArray *stops = [NSMutableArray arrayWithCapacity:20];
	
	NSString *url = [NSString stringWithFormat:@"http://dynamic.12306.cn/TrainQuery/skbcx.jsp?cxlx=cc&date=%@&trainCode=%@", dt, trainCode];
	NSString *content = [self fetchUrl:url];
	// NSLog(@"%@", content);

	if(content == nil){
		return nil;
	}
	
	NSArray *matches = [QRegExp matchAll:@"mygrid\\.addRow\\(.*\"(.*)\".*\\);" withString:content flags:(REG_EXTENDED | REG_NEWLINE)];

	/*
	[content release];
	[url release];
	*/
	
	if(matches == nil){
		return nil;
	}
	
	// NSLog(@"maches %@", matches);
	NSEnumerator *enumerator = [matches objectEnumerator];
	NSArray *match;
	while(match = [enumerator nextObject]){
		// NSLog(@"%@", match);
		NSString *stop = [match objectAtIndex:1];
		// NSLog(@"%@", stop);
		
		NSMutableArray *fields = [NSMutableArray arrayWithArray:[stop componentsSeparatedByString:@","]];
		// NSLog(@"%@", fields);
		NSString *stopName = [[[fields objectAtIndex:1] componentsSeparatedByString:@"^"] objectAtIndex:0];
		[fields replaceObjectAtIndex:1 withObject:stopName];
		
		int count = [fields count];
		int i;
		for(i=0; i<count; i++){
			NSString *sss = [fields objectAtIndex:i];
			
			sss = [sss stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
			
			[fields replaceObjectAtIndex:i withObject:sss];
		}
		/*
		NSString *trainCode = [fields objectAtIndex:2];
		NSString *timeArrive = [fields objectAtIndex:3];
		NSString *timeDeparture = [fields objectAtIndex:4];
		NSString *timeTotal = [fields objectAtIndex:5];
		NSString *priceYZ = [fields objectAtIndex:6];
		NSString *priceRZ = [fields objectAtIndex:7];
		NSString *priceYW = [fields objectAtIndex:8];
		NSString *priceRW = [fields objectAtIndex:9];
		NSString *priceYD = [fields objectAtIndex:10];
		NSString *priceED = [fields objectAtIndex:11];
		NSString *priceGR = [fields objectAtIndex:12];
		*/
		
		// NSLog(@"%@", stopName);
	
		[stops addObject:fields];
	}
	
	/*
	[matches release];
	*/
	
	return [NSArray arrayWithArray:stops];
}



+ (NSString *)fetchUrl:(NSString *)url {
	
	NSLog(@"fetch url: %@", url);
	
	NSString *content = nil;
	
	NSError *error;
	
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
	[request setTimeoutInterval:10.0];
	[request setValue:@"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3" forHTTPHeaderField:@"User-Agent"];
	[request setValue:@"http://dynamic.12306.cn/TrainQuery/skbcx.jsp?cxlx=cc&date=20100601&trainCode=6466" forHTTPHeaderField:@"Referer"];
	
	NSHTTPURLResponse *response;
	NSData *contentData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
	
	// NSLog(content);
	

	
	if([error code] != 0){
		NSLog(@"Error: %d, %@", [error code], [error localizedDescription]);
	
		/*
		[contentData release];
		[request release];
		[response release];
		*/
		
		return nil;
		
	} else {
		NSLog(@"fetch url: %@ done", url);
		
		
		NSString *tmp = [[NSString alloc] initWithData:contentData encoding:NSUTF8StringEncoding];
		content = [NSString stringWithString:tmp];
		[tmp release];
		
		/*
		[contentData release];
		[request release];
		[response release];
		*/
		
		return content;
	}

}

@end
